package grape.workflow.rest.flowable.ui;

import grape.common.AbstractLoginUser;
import grape.common.service.loginuser.LoginUser;
import org.flowable.idm.api.User;
import org.flowable.ui.common.model.RemoteUser;
import org.flowable.ui.common.security.DefaultPrivileges;
import org.flowable.ui.common.security.SecurityUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangwei
 * Created at 2020/6/3 16:18
 */
public class FlowableUserInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 添加用户
        LoginUser loginUser = AbstractLoginUser.getLoginUser();
        if (loginUser != null) {
            RemoteUser user = new RemoteUser();
            user.setId(loginUser.getId());
            List<String> pris = new ArrayList<>();
            pris.add(DefaultPrivileges.ACCESS_MODELER);
            pris.add(DefaultPrivileges.ACCESS_IDM);
            pris.add(DefaultPrivileges.ACCESS_ADMIN);
            pris.add(DefaultPrivileges.ACCESS_TASK);
            pris.add(DefaultPrivileges.ACCESS_REST_API);
            user.setPrivileges(pris);
            SecurityUtils.assumeUser(user);
        }
        return super.preHandle(request, response, handler);
    }
}
